package fasilkom.antz.gui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import com.golden.gamedev.gui.TLabel;
import com.golden.gamedev.gui.theme.basic.BLabelRenderer;
import com.golden.gamedev.gui.toolkit.GraphicsUtil;
import com.golden.gamedev.gui.toolkit.TComponent;
import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.util.ImageUtil;

public class CustomLabelRenderer extends BLabelRenderer {
	
	@Override
	public BufferedImage[] createUI(TComponent component, int w, int h) {
		BufferedImage[] ui = new BufferedImage[2];

		String[] bgColor = new String[] {
			"Background Color", "Background Disabled Color"
		};

		for (int i=0;i < ui.length;i++) {
			Color background = (Color) get(bgColor[i], component);

			ui[i] = ImageUtil.createImage(w, h, Transparency.TRANSLUCENT);

			Graphics2D g = ui[i].createGraphics();

			// fill background
			if (background != null) {
				g.setColor(background);
				g.fillRect(0, 0, w, h);
			}

			// draw border
			Color border = (Color) get("Background Border Color", component);
			if (border != null) {
				g.setColor(border);
				g.drawRect(0, 0, w-1, h-1);
			}

			g.dispose();
		}

		return ui;
	}
	
	public void processUI(TComponent component, BufferedImage[] ui) {
		TLabel label = (TLabel) component;

		String[] color = new String[] { "Text Color", "Text Disabled Color" };
		String[] font = new String[] { "Text Font", "Text Disabled Font" };

		String[] document = GraphicsUtil.parseString(label.getText());
		for (int i=0;i < 2;i++) {
			Graphics2D g = ui[i].createGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					   RenderingHints.VALUE_ANTIALIAS_ON);
			GraphicsUtil.drawString(g, document,
									label.getWidth(), label.getHeight(),
									(GameFont) get(font[i], component),
									(Color) get(color[i], component),
									(Integer) get("Text Horizontal Alignment Integer", component),
									(Integer) get("Text Vertical Alignment Integer", component),
									(Insets) get("Text Insets", component),
									(Integer) get("Text Vertical Space Integer", component));
			g.dispose();
		}
	}
}
